//
//  ResponseData.m
//  Cabs
//
//  Created by Joey on 2018/5/15.
//  Copyright © 2018年 Happyev. All rights reserved.
//

#import "ResponseData.h"
#import "BaseReturnModel.h"
#import "MeRegisterVC.h"

@implementation ResponseData

+ (instancetype)responseDataWithData:(id)data reponseObjctClass:(Class)reponseObjctClass{
    
    ResponseData *response = [[self alloc] init];
    //原始数据
    response-> _responseRawData = data;
    //处理后的数据
    id newData = [self changeType:data];
    
    @try {
        //解析错误码
        /* FIXME: ================= 需要吧所有之前数组或者手动解析 全部修改 这里判断如果yyModel解析失败 就直接中断解析 返回error 解析失败 ================= */

        if ([newData isKindOfClass:[NSDictionary class]]||[newData isKindOfClass:[NSArray class]]) {
            
            //无对应的key  rawCode 直接为0
            NSString *rawCode = newData[@"code"];

            if ([rawCode isEqualToString:@"00"]) {
            
                response-> _success = YES;
                id responseClass = nil;

                if ([newData[@"data"] isKindOfClass:[NSArray class]]){
                    responseClass = [reponseObjctClass yy_modelWithJSON:newData];

                }else{
                    responseClass = [reponseObjctClass yy_modelWithJSON:newData[@"data"] ];
                }
                
                //使用YYModel解析json

                if (responseClass) {

                    response-> _responseData = responseClass;
                }
                //部分接口不需要返回model
                if ([reponseObjctClass isKindOfClass:[BaseReturnModel class]]) {
                    response-> _responseData = newData[@"data"];
                }
                
            }else{
                if (![newData[@"code"] isEqualToString:@"402"]) {
                    //402不处理
                    if ([newData[@"msg"] isEqualToString:@"未登录"]) {
                        [Tools pushSinge];
                        //                    response-> _errorInfo = newData[@"msg"];
                    }else{
                        response-> _errorInfo = newData[@"msg"];
                    }
                }
             
            }
        
          
        } else {
            
            response-> _errorInfo = [NSString stringWithFormat:@"服务返回数据格式错误,返回的数据类型为:%@",[newData class]];
            
        }
    } @catch (NSException *exception) {
        DLog(@"程序崩了,数据解析失败,请检查(instancetype)responseDataWithData:(id)data");
    } @finally {
        
    }

    return response;
}

-(void)setDicData:(NSDictionary*)newData{
    
}




-(void)setData:(Class)reponseObjctClass responseObject:(id)responseObject{
 
}

+ (instancetype)responseDataWithError:(NSError *)error {
    ResponseData *response = [[self alloc] init];
    response-> _errorInfo = [self errorDescription:error];
    return response;
}

+ (NSString *) errorDescription:(NSError *) error{
    NSString * description = error.localizedDescription;
    NSString * domain = error.domain;
    NSInteger code = error.code;
    
    if ([NSURLErrorDomain isEqualToString:domain] || [AFURLResponseSerializationErrorDomain isEqualToString:domain]){
        switch (code) {
            case NSURLErrorTimedOut:
                description = @"当前网络状况不佳，请检查网络是否开启";
                break;
            case NSURLErrorCannotConnectToHost:
            case NSURLErrorCannotFindHost:
                description = @"当前网络状况不佳，请检查网络是否开启";
                break;
            case NSURLErrorNetworkConnectionLost:
            case NSURLErrorNotConnectedToInternet:
                description = @"无法连接到网络，请检查网络设置";
                break;
            case NSURLErrorCannotParseResponse:
                description = @"数据解析出错";
                break;
            case NSURLErrorResourceUnavailable:
            case NSURLErrorFileDoesNotExist:
                description = @"您访问的内容不可用";//404之类
                break;
            case NSURLErrorBadServerResponse:
                description = @"访问服务器遇到错误，请稍后重试";
                break;
            case NSURLErrorCancelled:
                description = @"当前网络请求被取消";
                break;
            default:
                description = @"网络未知错误";
                break;
        }
    }
    
    return description;
}


+ (NSDictionary *)nullDic:(NSMutableDictionary *)myDic {
    
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc] init];
    for (int i = 0; i < keyArr.count; i ++) {
        
        id obj = [myDic objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

//将NSArray中的Null类型的项目转化成@""
+ (NSArray *)nullArr:(NSArray *)myArr {
    
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++) {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
    }
    return resArr;
}

//将NSString类型的原路返回
+ (NSString *)stringToString:(NSString *)string {
    
    return string;
}

//将Null类型的项目转化成@""
+(NSString *)nullToString {
    
    return @"";
}

//主要方法
//类型识别:将所有的NSNull类型转化成@""
+ (id)changeType:(id)myObj {
    if ([myObj isKindOfClass:[NSDictionary class]]) {
        
        return [self nullDic:myObj];
        
    } else if ([myObj isKindOfClass:[NSArray class]]) {
        // NSArray * arr = [NSArray new];
        
        return [self nullArr:myObj];
        
    } else if ([myObj isKindOfClass:[NSString class]]) {
        
        return [self stringToString:myObj];
        
    } else if ([myObj isKindOfClass:[NSNull class]]) {
        
        return [self nullToString];
        
    } else {
        
        return myObj;
        
    }
}

- (NSString *)description {
    return [NSString stringWithFormat:@"请求结果==%d\n处理后的数据:%@\n 原始数据:%@\n错误信息==%@",self.success,self.responseData,self.responseRawData,self.errorInfo];
}


@end
